﻿using System;
using System.Collections.Generic;
using eShop.Business;
using Ext.Net;
using eShop.Model;

public partial class admin_inventory_report : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
            Session["PrintOrderList"] = null;
        }
    }
    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindOrders(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);

    }

    private void BindOrders(string sort, int start, int limit)
    {
        List<ProductStock> data = null;
        int totalCount = 0;
        // start = start * limit;
        if (String.IsNullOrEmpty(sort))
            sort = "StockId desc";
        data = ProductStockManager.GetStockOutItemsPaged(10, sort, start, limit);
        totalCount = 10;
        Store1.DataSource = data;
        Store1.DataBind();
        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "EShop.WebService")]
    public void UpdateInventory(int id, string qty)
    {
        qty = qty.Trim();
        int quantity=0;
        Int32.TryParse(qty,out quantity);
        
        ProductStock _stock=ProductStockManager.GetStockById(id);
        if (_stock != null && quantity > 0)
        {
            _stock.StockAvailable = quantity;
            ProductStockManager.Update(_stock);
            Store1.DataBind();
        }       
        
    }

    [DirectMethod(Namespace = "EShop.WebService")]
    public void ExportExcell()
    {
        Session["report"] = "Inventory";
        Response.Redirect(SiteGlobals.BaseUrl + "admin/DownloadCSV.aspx");
    }
    [DirectMethod(Namespace = "EShop.WebService")]
    public void InsertNew()
    {
        Inventory.DeleteInventory();
        Inventory.InsertInventory();
        Store1.DataBind();
    }
}